import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';
import { TreeNode } from 'primeng/primeng';
import { Observable, Subject } from 'rxjs';
import { map } from "rxjs/operators";
import * as config from '../common/configuration';
import { BizType, ProcDef, ProcPart } from './freeflow.model';

@Injectable()
export class FreeflowService {

    /**用于在路由的环境下与父组件通讯 */
    private routeMsgPushSource = new Subject<string>();
    routeMsgPush$ = this.routeMsgPushSource.asObservable();

    constructor(private http: HttpClient) { }

    /** 获取业务节点信息 */
    listBizs() {
        return this.http.get('/freeflow/api/biz/list').pipe(
            map((res: Response) => res['data'])
        )
    }

    /**获取业务类型详细信息 */
    showBizDetail(id: number) {
        return this.http.get('/freeflow/api/biz/detail?bizId=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 删除业务类型 */
    deleteBiz(code: number) {
        return this.http.get('/freeflow/api/biz/delete?bizId=' + code).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 更新业务类型 */
    updateBiz(biz: BizType) {
        return this.http.post('/freeflow/api/biz/update', biz).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 获取流程定义树状图 */
    getProceDefTree(bizId: number, procDefId: number) {
        return this.http.get('/freeflow/api/procdef/listtree?bizId=' + bizId + '&procDefId='+procDefId).pipe(
            map((res: Response) => res['data'])
        )
    }


    /**获取流程定义相关信息 */
    showProcDefDetail(id: number) {
        return this.http.get('/freeflow/api/procdef/detail?id=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }

    /**更新或保存流程基本信息 */
    saveUpdateProc(proc: any) {
        return this.http.post('/freeflow/api/procdef/saveupdate', proc).pipe(
            map((res: Response) => res['data'])
        )
    }

    /**删除流程版本 */
    deleteProc(procDefId: number) {
        return this.http.get('/freeflow/api/procdef/delete?id=' + procDefId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 加载流程节点信息 */
    listProcActDef(procDefId: number) {
        return this.http.get('/freeflow/api/procactdef/list?id=' + procDefId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 创建流程节点信息*/
    createProcAct(act: any) {
        return this.http.post('/freeflow/api/procactdef/create', act).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 获取流程节点信息 */
    loadProcActDef(actDefId: number) {
        return this.http.get('/freeflow/api/procactdef/load?actDefId=' + actDefId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 获取流程节点参与者 */
    listProcDefActParts(actDefId: number) {
        return this.http.get('/freeflow/api/procpart/listactparts?actDefId=' + actDefId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 移动节点位置 */
    moveProcActDef(act: any) {
        return this.http.post('/freeflow/api/procactdef/move', act).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 加载字典属性信息 */
    loadDict(){
        return this.http.get('/freeflow/api/dict/actattr').pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 更新节点信息 */
    updateProcActDef(act: any) {
        return this.http.post('/freeflow/api/procactdef/saveupdate', act).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 更新节点参与者 */
    saveUpdateActPart(actDefId: number, partIds: string) {
        return this.http.get('/freeflow/api/procpart/saveactparts?actDefId=' + actDefId + '&partIds=' + partIds).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 删除节点信息 */
    deleteActDef(actDefId: number) {
        return this.http.get('/freeflow/api/procactdef/delete?id=' + actDefId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 节点过滤信息 */
    listProcActPartFilters(actDefId: number) {
        return this.http.get('/freeflow/api/procfilter/listactparts?actDefId=' + actDefId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 保存节点参与者过滤信息 */
    saveUpdateActPartFilter(actPartFilter: any) {
        return this.http.post('/freeflow/api/procfilter/saveactfilter', actPartFilter).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 删除节点过滤信息 */
    removeFilter(actDefId: number, partId: number){
        return this.http.get('/freeflow/api/procfilter/delete?actDefId=' + actDefId + '&partId='+ partId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 获取参与者过滤信息 */
    loadUpdateActPartFilter(actDefId: number, partId: number) {
        return this.http.get('/freeflow/api/procfilter/loadactpartfilter?actDefId=' + actDefId + "&partId=" + partId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 创建环节连线 */
    createActTrans(transDef: any) {
        return this.http.post('/freeflow/api/proctransdef/create', transDef).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 加载流向信息 */
    listProcTrans(procDefId: number) {
        return this.http.get('/freeflow/api/proctransdef/list?procDefId=' + procDefId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载流向信息 */
    loadProcTrans(transId: number) {
        return this.http.get('/freeflow/api/proctransdef/load?id=' + transId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 删除流向 */
    deleteProcTran(id: number) {
        return this.http.get('/freeflow/api/proctransdef/delete?id=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 更新流向信息 */
    updateProcTrans(trans: any) {
        return this.http.post('/freeflow/api/proctransdef/update', trans).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 流程参与者信息 */
    listProcParts(id: number) {
        return this.http.get('/freeflow/api/procpart/list?id=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 删除参与者信息 */
    deleteProcPart(part: ProcPart) {
        return this.http.get('/freeflow/api/procpart/delete?procDefId=' + part.procDefId + '&partId=' + part.partId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 新郑流程参与者信息 */
    addProcPart(parts: ProcPart[]) {
        return this.http.post('/freeflow/api/procpart/add', parts).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载环节事件信息 */
    loadProcActEvents(actDefId: number){
        return this.http.get('/freeflow/api/procevent/list?actDefId=' + actDefId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 保存或更新环节事件信息 */
    saveUpdateEvent(actEvent: any){
        return this.http.post('/freeflow/api/procevent/saveupdate', actEvent).pipe(
            map((res: Response) => res['data'])
        )
    }
    
    /** 删除环节事件 */
    deleteActEvent(eventId: number){
        return this.http.get('/freeflow/api/procevent/delete?eventId='+eventId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /**父子组件通讯 */
    announceMission(data: string) {
        this.routeMsgPushSource.next(data);
    }
}